package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.handler.CustomException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.bouncycastle.cms.PasswordRecipientId;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author test.java
 * @since 2023-03-11
 */
@Api(value="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin //解决跨域的问题
public class EduTeacherController {

	@Resource
	private EduTeacherService eduTeacherService;

	@Resource
	private EduCourseService eduCourseService;


	@ApiOperation(value = "所有讲师列表")
	@PostMapping("/findAll")
	public Result teachers(){
		List<EduTeacher> list = eduTeacherService.list(null);

		return Result.ok().data("list",list);
	}

     // todo 用户端查询讲师课程
	@ApiOperation(value = "根据ID查询讲师")
	@GetMapping(value = "{id}")
	public Result getById(
			@ApiParam(name = "id", value = "讲师ID", required = true)
			@PathVariable String id){

		//查询讲师信息
		EduTeacher teacher = eduTeacherService.getById(id);

		//根据讲师id查询这个讲师的课程列表
		LambdaQueryWrapper<EduCourse> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(EduCourse::getTeacherId,id);
		List<EduCourse> courseList = eduCourseService.list(lambdaQueryWrapper);
		return Result.ok().data("teacher", teacher).data("courseList", courseList);
	}



	@ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/{id}")
	public Result delete(@PathVariable("id") String id){
		boolean b = eduTeacherService.removeById(id);
		if (b){
			return Result.ok();
		}else{
			return Result.error();
		}
	}

	@ApiOperation(value = "分页讲师列表")
	@GetMapping("{page}/{limit}")
	public Result pageList(
			@ApiParam(name = "page", value = "当前页码", required = true)
			@PathVariable Long page,

			@ApiParam(name = "limit", value = "每页记录数", required = true)
			@PathVariable Long limit){

		//  分页查询的返回结果为查询的数据-》records 和 数据条数=>total
		try{
			Page<EduTeacher> pageParam = new Page<>(page, limit);

			eduTeacherService.page(pageParam, null);
			List<EduTeacher> records = pageParam.getRecords();
			long total = pageParam.getTotal();
			return  Result.ok().data("total", total).data("rows", records);
		}catch (Exception exception){
			throw new CustomException(20001,"程序异常");
		}
	}

	@PostMapping("/pageTeacherByCondition/{current}/{limit}")
	public Result pageTeacherByCondition(@PathVariable Long current, @PathVariable Long limit,@RequestBody(required = false) TeacherQuery teacherQuery){
		Page<EduTeacher> page=new Page<>(current,limit);

		Page<EduTeacher>eduTeacherPage = eduTeacherService.pageTeacherByCondition(page,teacherQuery);

		List<EduTeacher> records = eduTeacherPage.getRecords();
		long total = eduTeacherPage.getTotal();

		return  Result.ok().data("total", total).data("rows", records);

	}

	@ApiOperation(value = "根据ID修改讲师")
	@PostMapping("/{id}")
	public Result updateById(
			@ApiParam(name = "id", value = "讲师ID", required = true)
			@PathVariable String id,
			@ApiParam(name = "teacher", value = "讲师对象", required = true)
			@RequestBody EduTeacher teacher){

		teacher.setId(id);
		eduTeacherService.updateById(teacher);
		
		return Result.ok();
	}

	@ApiOperation(value = "新增讲师")
	@PostMapping("/add")
	public Result save(
			@ApiParam(name = "teacher", value = "讲师对象", required = true)
			@RequestBody EduTeacher teacher){
		boolean save = eduTeacherService.save(teacher);
		if (save){
			return Result.ok();
		}else {
			return Result.error();
		}
	}
}

